package cn.com.haopy.yue.ai.wx.service;


import cn.com.haopy.yue.ai.core.config.PdfAiApiProperties;
import cn.com.haopy.yue.ai.core.utils.HttpUtils;
import cn.com.haopy.yue.ai.wx.dto.AIPdfResDto;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 风险评估报告地址pdf
 */
@Service
@Slf4j
public class PdfAiApiService {

    @Autowired
    private PdfAiApiProperties pdfAiApiProperties;

    String path = "/getPdf/";


    private static Map<String, Object> toMap(Object entity) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = entity.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true); // 使得私有字段也可以访问
            try {
                map.put(field.getName(), field.get(entity));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public Map<String, String> handHeader() {
        //头部签名
        Map<String, String> header = new HashMap<>();
        System.out.println("头：" + "");
        header.put("Authorization", "222");
        return header;
    }


    public AIPdfResDto createPdf(JSONObject json) throws Exception {
        Map<String, Object> params = JSONObject.toJavaObject(json, Map.class);
        JSONObject data = HttpUtils.postJSON(pdfAiApiProperties.getUrl() + path, null, params);
        return handleResult(data);
    }


    @SuppressWarnings({"rawtypes", "unchecked"})
    private AIPdfResDto handleResult(JSONObject data) throws Exception {
        System.out.println("pdf ret:" + data);
        if (data == null) {
            throw new Exception("pdf create 返回数据错误");
        }
        AIPdfResDto chartMsgResDto = data.toJavaObject(AIPdfResDto.class);
        chartMsgResDto.setUrl(pdfAiApiProperties.getUrl() + chartMsgResDto.getUrl());

        System.out.println(chartMsgResDto);
        return chartMsgResDto;
    }
}
